package com.furschina.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.URL;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "cache.daily")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Indexed
@Analyzer(impl = SmartChineseAnalyzer.class)
public class Article extends BaseModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1897349951875578875L;
	@Column(nullable = false)
	@Length(max = 40, min = 4)
	@NotNull
	@Field(index = Index.TOKENIZED)
	private String title;
	@Length(max = 200, min = 4)
	@NotNull
	private String summary;
	@URL
	@Length(max = 255)
	private String url; // 重定向链接
	@Column(length = 10000)
	@Length(max = 10000)
	@Field(index = Index.TOKENIZED)
	private String content;
	@Temporal(TemporalType.TIMESTAMP)
	private Date publishDate;
	private boolean published = true;
	@ManyToOne(fetch = FetchType.LAZY)
	@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
	@NotNull
	private Channel channel;
	private String cover; // 资讯封面
	@OneToOne(mappedBy = "article", orphanRemoval = true)
	@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
	private ArticleRecord record;
	@Field(index = Index.TOKENIZED, analyzer = @Analyzer(impl = WhitespaceAnalyzer.class))
	private String tag; // 关键字

	/**
	 * @return the tag
	 */
	public String getTag() {
		return tag;
	}

	/**
	 * @param tag
	 *            the tag to set
	 */
	public void setTag(String tag) {
		this.tag = tag;
	}

	/**
	 * @return the record
	 */
	public ArticleRecord getRecord() {
		return record;
	}

	/**
	 * @param record
	 *            the record to set
	 */
	public void setRecord(ArticleRecord record) {
		this.record = record;
	}

	/**
	 * @return the cover
	 */
	public String getCover() {
		return cover;
	}

	/**
	 * @param cover
	 *            the cover to set
	 */
	public void setCover(String cover) {
		this.cover = cover;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the channel
	 */
	public Channel getChannel() {
		return channel;
	}

	/**
	 * @param channel
	 *            the channel to set
	 */
	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}

	public boolean isPublished() {
		return published;
	}

	public void setPublished(boolean published) {
		this.published = published;
	}

	@Override
	public String toString() {
		return "Article [title=" + title + ", summary=" + summary
				+ ", content=" + content + ", publishDate=" + publishDate
				+ ", published=" + published + "]";
	}

}
